﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Lidgren.Network;

namespace NetworkDemo
{
    public class NetworkPopupScreen : ScreenPopup
    {
        NetworkManager netMan;
        double timeAtStart = -1.0f;
        string curNetStatus = "Connecting";
        bool connecting = false;
        public NetworkPopupScreen(Texture2D background, Texture2D border, NetworkManager netMan)
            : base(background, border, new Rectangle(500, 200, 350, 150))
        {
            this.IsPopup = true;
            this.netMan = netMan;
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            if (IsActive)
            {
                if (connecting)
                {
                    if (gameTime.TotalRealTime.TotalSeconds - timeAtStart > 20.0f)
                    {
                        connecting = false;
                        timeAtStart = gameTime.TotalRealTime.TotalSeconds;
                    }
                }
                else if (timeAtStart > 0.0f)
                {
                    if (gameTime.TotalRealTime.TotalSeconds - timeAtStart > 5.0f)
                    {
                        ExitScreen();
                    }
                }
                else if (timeAtStart == -1.0f)
                {
                    StartConnecting(gameTime.TotalRealTime.TotalSeconds);
                }
            }
        }

        public void StartConnecting(double seconds)
        {
            timeAtStart = seconds;
            connecting = true;
            netMan.ConnectToServer("85.235.20.111", 1000);
        }

        public override void HandleInput(InputState input)
        {
            // Move to the previous menu entry?
            if (input.IsNewKeyPress(Keys.Escape))
            {
                ExitScreen();
            }
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            Vector2 Area = new Vector2((float)PopupSize.X, (float)PopupSize.Y);
            if (connecting)
            {
                spriteBatch.Begin();
                spriteBatch.DrawString(ScreenManager.Font, curNetStatus + " ... (Hit 'esc' to cancel)", new Vector2(Area.X + 35.0f, Area.Y + 30.0f), Color.Black);
                spriteBatch.DrawString(ScreenManager.Font, "Time elapsed: " + (int)(gameTime.TotalRealTime.TotalSeconds - timeAtStart), new Vector2(Area.X + 35.0f, Area.Y + 65.0f), Color.Black);
                spriteBatch.End();
            }
            else if (timeAtStart > 0.0f)
            {
                spriteBatch.Begin();
                spriteBatch.DrawString(ScreenManager.Font, "Connection failed.", new Vector2(Area.X + 35.0f, Area.Y + 30.0f), Color.Red);
                spriteBatch.End();
            }
        }
    }
}
